
#include "CSound.h"

CSound CSound::soundControl;
std::vector<Mix_Chunk*> CSound::soundList;

CSound::CSound(){
	chunk = NULL;
}

int CSound::onLoad(char *file){
	
	chunk = NULL;

	if( (chunk = Mix_LoadWAV(file)) == NULL )
		return -1;

	soundList.push_back(chunk);

	return (soundList.size() - 1);
}

void CSound::onCleanup(){

	if(chunk)
		Mix_FreeChunk(chunk);

	chunk = NULL;
}

void CSound::play(unsigned int id){
	
	if(id < 0 || id >= soundList.size())
		return;

	if(soundList[id] == NULL)
		return;

	Mix_PlayChannel(-1, soundList[id], 0);
}

void CSound::setVolume(unsigned int v){
	
	if(v < 0 || v > MIX_MAX_VOLUME)
		return;

	unsigned int i;

	for(i=0; i< soundList.size(); i++)
		if(chunk == soundList[i])
		{
			Mix_VolumeChunk(chunk,v);
			break;
		}
}